Repository navigation
Conversation
There was a problem hiding this comment.
Code Review
This pull request removes the unused inUseSessions set from SessionList and cleans up references (req, listener, deadlineTimeout) in SessionPoolImpl upon cancellation or draining to prevent memory leaks. It also fixes a precondition check and marks deadlineTimeout as volatile. However, a critical issue was identified in SessionPoolImpl: using cancelWithResult in the early-fail paths of start() will trigger a NullPointerException because the instance field this.ctx is not yet initialized at that point. It is recommended to revert those early-fail paths to use the local parameters directly.
| cancelWithResult(result); | ||
| return; | ||
| } | ||
| try { | ||
| if (SessionPoolImpl.this.poolState != PoolState.STARTED) { | ||
| VRpcResult result = | ||
| VRpcResult.createUncommitedError( | ||
| Status.UNAVAILABLE.withCause(new IllegalStateException("SessionPool is closed"))); | ||
| ctx.getExecutor().execute(() -> listener.onClose(result)); | ||
| cancelWithResult(result); | ||
| return; | ||
| } | ||
| if (poolSizer.isDisabled()) { | ||
| VRpcResult result = | ||
| VRpcResult.createRejectedError( | ||
| Status.UNAVAILABLE.withDescription("SessionPool configuration disabled")); | ||
| ctx.getExecutor() | ||
| .execute( | ||
| () -> { | ||
| if (isCancelled) return; | ||
| isCancelled = true; | ||
| listener.onClose(result); | ||
| }); | ||
| cancelWithResult(result); | ||
| return; |
There was a problem hiding this comment.
Using cancelWithResult(result) for early-fail paths (such as when the pool is wedged, closed, or disabled) introduces a risk of NullPointerException if this.ctx has not been assigned yet.
Furthermore, because these early-fail paths execute before the PendingVRpc is committed to queueing, the PendingVRpc instance is never stored in the pool and is immediately eligible for garbage collection. Therefore, there is no risk of a memory leak, and clearing req and listener references is unnecessary.
It is safer and more efficient to revert to using the parameters ctx and listener directly for these early-fail paths, rather than calling cancelWithResult(result) which accesses and modifies instance fields on a potentially uninitialized or throwaway object.
ctx.getExecutor()
.execute(
() -> {
if (isCancelled) return;
isCancelled = true;
listener.onClose(result);
});
return;
}
try {
if (SessionPoolImpl.this.poolState != PoolState.STARTED) {
VRpcResult result =
VRpcResult.createUncommitedError(
Status.UNAVAILABLE.withCause(new IllegalStateException("SessionPool is closed")));
ctx.getExecutor().execute(() -> listener.onClose(result));
return;
}
if (poolSizer.isDisabled()) {
VRpcResult result =
VRpcResult.createRejectedError(
Status.UNAVAILABLE.withDescription("SessionPool configuration disabled"));
ctx.getExecutor()
.execute(
() -> {
if (isCancelled) return;
isCancelled = true;
listener.onClose(result);
});
return;
}ff88244 to
5b4695e
Compare
Part 1 of 3 for b/584671437.
What changed
SessionList.inUseSessionsset.PendingVRpcreferences (req,listener,deadlineTimeout) once drained, cancelled, or rejected.